package com.wr.controllers;

import com.wr.pojos.PermissionEntity;
import com.wr.pojos.RolePermissionEntity;
import com.wr.services.PermissionService;
import com.wr.utils.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @RequestMapping("/getPermissions")
    public String getPermissions(PageInfo pageInfo, Model model){
        List<PermissionEntity> list = permissionService.selectPermissionList(pageInfo);
        model.addAttribute("list",list);
        return "layui/permissionManage/permission_list";
    }

    @RequestMapping("/getRolePermission")
    public String getRolePermission(int roleId,Model model){
        List<PermissionEntity> lists = permissionService.selectPermissionList();
        List<RolePermissionEntity> rolePermissions = permissionService.selectRolePermissions(roleId);
        List list = new ArrayList();
        for(int i=0;i<rolePermissions.size();i++){
            list.add(rolePermissions.get(i).getPermissionId());
        }
        model.addAttribute("rolePermissions",rolePermissions);
        model.addAttribute("lists",lists);
        model.addAttribute("rolePermissionstr",list);
        return "layui/roleManage/popup_layer";
    }

    @RequestMapping("/getPermission")
    public String getPermission(Model model){
        List<PermissionEntity> lists = permissionService.selectPermissionList();
        model.addAttribute("lists",lists);
        return "layui/roleManage/addRole_popup";
    }
}
